/*
 * @Author: jianghaiyang807
 * @Date:   2016-10-27
 */

import Vue from 'vue'
import VueResource from 'vue-resource'

Vue.use(VueResource);
const resource = new VueResource({})

const timestamp = location.search.match(/\?/) ? '' : '?'
Vue.http.interceptors.push(function (request, next) {
  request.timeout = 30000
  request.url += timestamp
  if (request.body && !request.body.noLoading) {
    this.showLoading()
  }
  // // 当请求状态为canceled时，不进入回调函数，手动清除loading状态
  let timer = setTimeout(() => {
    this.hideLoading()
     next({})
  }, request.timeout)
  next((response) => {
    this.hideLoading()
    clearTimeout(timer)
    return response
  })
})
// credentials 标识
Vue.http.options.credentials = false
Vue.http.options.emulateJSON = true
Vue.http.options.emulateHTTP = true

export default resource